package com.cnap.multiclouldmgmt.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnap.multiclouldmgmt.model.dockerHarborModel.*;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterBasicModel;
import com.cnap.multiclouldmgmt.model.vm.VmCreateModel;
import com.cnap.multiclouldmgmt.model.vm.VmModel;
import com.cnap.multiclouldmgmt.service.CnapSshService;
import com.cnap.multiclouldmgmt.service.DockerHarborService;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @author dyq
 * docker harbor 操作
 *  * 200
 *  * The secret is successfully updated
 *  *
 *  * 400
 *  * Invalid user ID. Or user is not onboarded via OIDC authentication. Or the secret does not meet the standard.
 *  *
 *  * 401
 *  * User need to log in first.
 *  *
 *  * 403
 *  * Non-admin user can only generate the cli secret of himself.
 *  *
 *  * 404
 *  * User ID does not exist.
 *  *
 *  * 412
 *  * The auth mode of the system is not "oidc_auth", or the user is not onboarded via OIDC AuthN.
 *  *
 *  * 500
 *
 *  0.概述
 * 核心管理API提供了Harbor核心管理功能的编程接口，这些功能主要如下。
 * ◎ 用户管理(“/users”和“/usergroups”): 覆盖用户和用户组相关的管理功能，包括用户和用户组的创建、修改、查找、删除等。
 * ◎ 项目管理(“/projects”): 覆盖项目相关的管理功能，包括项目的创建、修改、查找、获取概要、删除和项目元信息的管理等。
 * ◎ 仓库管理(“/projects/｛project_name｝/repositories”): 覆盖仓库相关的管理功能，包括仓库的修改、查找和删除等。
 * ◎ Artifact 管理( “/projects/ ｛ project_name ｝ /repositories/｛repository_name｝/artifacts”): 覆盖Artifact相关的管理功能，包括Artifact查找、删除、添加；标签移除；附加属性获取；Tag管理等。
 * ◎ 远程复制（“/replication”和“/registries”）：覆盖远程复制相关的功能，包括仓库服务实例管理及远程复制策略的管理、执行等。
 * ◎ 扫描（“/scanners”“/projects/｛project_id｝/scanner”和“/projects/｛ project_name ｝ /repositories/ ｛ repository_name ｝ /artifacts/｛reference｝/scan”等）：覆盖扫描相关的功能，包括扫描器管理、触发扫描和查看扫描结果等。
 * ◎ 垃圾回收（“/system/gc”）：覆盖垃圾回收相关的功能，包括触发垃圾回收和查看执行结果等。
 * ◎ 项目配额（“/quotas”）：覆盖项目配额相关的功能，包括项目配额的设置、更改和查看等。
 * ◎ Tag保留（“/retentions”）：覆盖Artifact保留策略相关的功能，包括保留策略的创建、修改、删除和执行等。
 * ◎ Artifact管理（“/projects/｛project_id｝/immutabletagrules”）：覆盖项目中不可变Artifact策略相关的功能，包括不可变策略的创建、修改、删除和执行等。
 * ◎ Webhook（“/projects/｛project_id｝/webhook”）：覆盖Webhook相关的功能，包括Webhook的创建、修改和删除等。
 * ◎ 系统配置（“/configurations”和“/systeminfo”）：覆盖系统配置和基本信息相关的功能，包括系统配置的查看和修改等。
 */
@Service("DockerHarborServiceImpl")
public class DockerHarborServiceImpl implements DockerHarborService {

    private final static Logger LOGGER = LoggerFactory.getLogger(DockerHarborServiceImpl.class);

    private final static String REGISTRY_URL = "/api/v2.0/registries";

    private final static String POLICY_URL = "/api/v2.0/replication/policies";

    private final static String PROJECT_URL = "/api/v2.0/projects";

    private final static String PROJECT_PREFIX = "test/";

    private final static String COPY_URL = "/api/v2.0/replication/executions";

    private  String DOCKER_URL;

    //中心docker服务器地址ip-高可用01
    @Value("${docker.url}")
    private String dockerMasterUrl;

    //中心docker服务器地址ip-高可用02
    @Value("${docker.worker}")
    private String dockerWorkerUrl;

    @Value("${docker.userName}")
    private  String principal;

    @Value("${docker.passwd}")
    private String password;

    @Autowired
    private MultiCloudMgmtService multiCloudMgmtService;

    @Autowired
    private CnapSshService cnapSshService;

    private static CloseableHttpClient httpClient;
    private static CloseableHttpClient getHttpClient(SSLContext sslContext){
        if (httpClient == null){
            httpClient = HttpClients.custom()
                    .setSSLContext(sslContext)
                    .setSSLHostnameVerifier(new NoopHostnameVerifier())
                    .build();

        }

        return httpClient;
    }
    public static void setHttpClient(CloseableHttpClient newHttpClient){

        httpClient = newHttpClient;
    }

    @Override
    public void delClusterImage(String clusterName, String imageInfo) {

        //获取集群的所有节点
        K8sClusterBasicModel k8sClusterBasicModel = multiCloudMgmtService.getK8sClusterInfoByName(clusterName);
        if (k8sClusterBasicModel != null) {
            List<VmModel> vmModels = k8sClusterBasicModel.getVmModels();
            for (VmModel vmModel : vmModels) {
                cnapSshService.delNodeImage(vmModel, imageInfo);
            }
        }
    }

    @Override
    public void delProjectImages(String k8sClusterIp) throws Exception {
        //获取一个对应的客户端
        K8sClusterBasicModel k8sClusterBasicModel = multiCloudMgmtService.getK8sClientByIp(k8sClusterIp);
        //获取这个客户端的相关情况
        if(StringUtils.isNotEmpty(k8sClusterBasicModel.getK8sClusterName())){
            VmModel workModel = null;
            VmModel masterModel = null;
            if ((k8sClusterBasicModel.getVmModels()!= null)
                    &&(!k8sClusterBasicModel.getVmModels().isEmpty())){
                for (VmModel vmModel:k8sClusterBasicModel.getVmModels()){
                    if (StringUtils.equals(vmModel.getK8sCluterRole(),CnapCloudMgmtConstants.CNAP_VM_MASTER)){
                        masterModel = vmModel;
                    }
                    if (StringUtils.equals(vmModel.getK8sCluterRole(),CnapCloudMgmtConstants.CNAP_VM_WORKER)){
                        workModel = vmModel;
                    }
                    if ((masterModel != null)&&(workModel != null)){
                        break;
                    }
                }

            }
            if (masterModel != null){
            multiCloudMgmtService.installK8sWorker(k8sClusterBasicModel.getK8sClusterName(),
                    masterModel,workModel,"ai",new VmCreateModel());
            VmCreateModel vmCreateModel = new VmCreateModel();
            vmCreateModel.setVmModels(new ArrayList<>());
            vmCreateModel.getVmModels().add(masterModel);
            vmCreateModel.getVmModels().add(workModel);
            vmCreateModel.setSvcId(masterModel.getBusinessId());
            vmCreateModel.setUserName(masterModel.getUserName());
            multiCloudMgmtService.installK8sFramwork(vmCreateModel);

            multiCloudMgmtService.updateVmJson(k8sClusterBasicModel,
                    JSONObject.toJSONString(k8sClusterBasicModel.getVmModels()));
            }

        }



    }

    @Override
    public void registerRemoteHarbor(String k8sClusterIp) throws UnsupportedEncodingException {

        //post-方法注册仓库管理
        String destUrl = "https://" + DOCKER_URL + REGISTRY_URL;
        HarborRegistryModel registryModel = new HarborRegistryModel();
        registryModel.setName(k8sClusterIp);
        registryModel.setUrl("https://" + k8sClusterIp);

        CredentialModel credentialModel = new CredentialModel();
        credentialModel.setAccess_key(principal);
        credentialModel.setAccess_secret(password);
        registryModel.setCredential(credentialModel);
        String param = JSON.toJSONString(registryModel);
        //注册仓库
        LOGGER.warn("registerRemoteHarbor ip={} param={}",k8sClusterIp,param);
        doHttpPostReq(destUrl,new StringEntity(param));
    }

    /**
     * 
     * @return
     */
    @Override
    public List<DestRegistryModel> getAllRegisterHarbor(String k8sClusterIp) {

        List<DestRegistryModel> result = new ArrayList<>();

        String destUrl = "https://" + DOCKER_URL + REGISTRY_URL;
        if (StringUtils.isNotEmpty(k8sClusterIp)){
            destUrl = "https://" + k8sClusterIp + REGISTRY_URL;
        }
        //请求到底有多少管理中的harbor
        LOGGER.warn("getAllRegisterHarbor desUrl={}",destUrl);
        String destStr = doHttpGetReq(destUrl);
        if (StringUtils.isEmpty(destStr)){
            return result;
        }
        List dests = JSONObject.parseObject(destStr, List.class);
        if ((dests == null)||dests.isEmpty()){
            return result;
        }
        for (Object obj: dests) {

            DestRegistryModel destRegistryModel = JSONObject.parseObject(
                    JSONObject.toJSONString(obj), DestRegistryModel.class);
            result.add(destRegistryModel);
            //展示一下所有的目标
            LOGGER.warn("getAllRegisterHarbor result destRegistryModel.getName={}",destRegistryModel.getName());

        }

        return result;
    }

    /**
     *
     * @param k8sClusterIp
     * @param destRegistryModel
     * @param projectName
     * @throws UnsupportedEncodingException
     */
    @Override
    public void addPolicy(String k8sClusterIp, DestRegistryModel destRegistryModel,String projectName) throws UnsupportedEncodingException {

        //注册规则
        String policyUrl = "https://" + DOCKER_URL + POLICY_URL;
        //ut
        if (StringUtils.equals(k8sClusterIp,CnapCloudMgmtConstants.TEST_ADMIN)){
            policyUrl =  "https://" + k8sClusterIp + POLICY_URL;
        }
        HarborPolicyModel harborPolicyModel = new HarborPolicyModel();
        harborPolicyModel.setName(k8sClusterIp);
        harborPolicyModel.setDest_registry(destRegistryModel);

        TriggerModel triggerModel = new TriggerModel();
        harborPolicyModel.setTrigger(triggerModel);

        FilterModel filterModel = new FilterModel();
        filterModel.setType("name");
        filterModel.setValue(projectName + "/**");
        harborPolicyModel.setFilters(new ArrayList<>());
        harborPolicyModel.getFilters().add(filterModel);

        String str = JSON.toJSONString(harborPolicyModel);
        doHttpPostReq(policyUrl,new StringEntity(str));
    }

    /**
     *
     * @param k8sClusterIp
     * @param projectName
     * @throws UnsupportedEncodingException
     */
    @Override
    public void addHarborProject(String k8sClusterIp, String projectName) throws UnsupportedEncodingException {

        String projectUrl = "https://" + k8sClusterIp + PROJECT_URL;

        ProjectModel projectModel = new ProjectModel();
        projectModel.setProject_name(projectName);
        Map<String,String> metadata = new HashMap<>();
        metadata.put("public","true");

        String str = JSON.toJSONString(projectModel);
        doHttpPostReq(projectUrl,new StringEntity(str));
        
    }

    /**
     *
     * @param k8sClusterIp
     * @return
     */
    @Override
    public List<ProjectInfoModel> getAllHarborProject(String k8sClusterIp) {

        List<ProjectInfoModel> result = new ArrayList<>();

        for (int i = 1;i < Integer.MAX_VALUE;i++) {

            String projectUrl = "https://" + k8sClusterIp + PROJECT_URL
                    + "?" + "page=" + i  + "&page_size=20";

            String projectsStr = doHttpGetReq(projectUrl);
            if (StringUtils.isEmpty(projectsStr)){
                return result;
            }
            List projects = JSONObject.parseObject(projectsStr, List.class);
            for (Object obj: projects) {

                result.add(JSONObject.parseObject(JSONObject.toJSONString(obj), ProjectInfoModel.class));
            }

            if(projects.size() < 20){
                break;
            }
        }

        return result;
    }

    /**
     * 
     * @param k8sClusterIp
     * @param userName
     * @throws IOException
     */
    @Override
    public void createClusterHarborCopyRule(String k8sClusterIp,String userName) throws IOException {

        //因为harbor高可用，需要把去两个harbor注册
        List<String> harborUrls = new ArrayList<>();
        harborUrls.add(dockerMasterUrl);
        if ((StringUtils.isNotEmpty(dockerWorkerUrl))
                &&(!StringUtils.equals(dockerWorkerUrl,dockerMasterUrl))){
            harborUrls.add(dockerWorkerUrl);
        }

        //只需要一个镜像仓库同步
        boolean syncExist = true;
        for (String dockerUrl:harborUrls) {

            DOCKER_URL = dockerUrl;

            //获取当前已经注册的仓库，如果已经存在就不需要进行仓库管理注册
            List<DestRegistryModel> oldDestRegistryModels = null;
            if (StringUtils.equals(k8sClusterIp, CnapCloudMgmtConstants.TEST_ADMIN)) {
                oldDestRegistryModels = new ArrayList<>();
                DestRegistryModel destRegistryModel = new DestRegistryModel();
                destRegistryModel.setName(CnapCloudMgmtConstants.TEST_ADMIN);
                oldDestRegistryModels.add(destRegistryModel);
            } else if (StringUtils.equals(k8sClusterIp, CnapCloudMgmtConstants.CNAP_VM_MASTER)) {
                oldDestRegistryModels = new ArrayList<>();
            } else {
                oldDestRegistryModels = getAllRegisterHarbor(null);
            }

            DestRegistryModel targetDestRegistryModel = null;
            for (DestRegistryModel destRegistryModel : oldDestRegistryModels) {
                if (StringUtils.equals(destRegistryModel.getName(), k8sClusterIp)) {
                    targetDestRegistryModel = destRegistryModel;
                }
            }

            //说明之前没注册过
            if (targetDestRegistryModel == null) {
                LOGGER.warn("begin to registerRemoteHarbor ip={}", k8sClusterIp);
                //post-方法注册仓库管理
                registerRemoteHarbor(k8sClusterIp);
                //获取当前已经注册的仓库，用来注册规则
                List<DestRegistryModel> destRegistryModels = null;
                if (StringUtils.equals(k8sClusterIp, CnapCloudMgmtConstants.CNAP_VM_MASTER)) {
                    destRegistryModels = new ArrayList<>();
                    DestRegistryModel destRegistryModel = new DestRegistryModel();
                    destRegistryModel.setName(CnapCloudMgmtConstants.CNAP_VM_MASTER);
                    destRegistryModels.add(destRegistryModel);
                } else {
                    destRegistryModels = getAllRegisterHarbor(null);
                }
                for (DestRegistryModel destRegistryModel : destRegistryModels) {

                    if (StringUtils.equals(destRegistryModel.getName(), k8sClusterIp)) {

                        targetDestRegistryModel = destRegistryModel;
                    }
                }
            }


            if (syncExist) {
                //先把之前的镜像手动同步一下
                syncExistImages(k8sClusterIp, targetDestRegistryModel, PROJECT_PREFIX + getMd5StringWith8(userName));
                syncExist = false;
            }

            //增加事件触发同步
            addPolicy(k8sClusterIp, targetDestRegistryModel, PROJECT_PREFIX + getMd5StringWith8(userName));
        }

    }

    @Override
    public List<PolicyModel> getAllPolicies(String dockerUrl,String k8sClusterIp) {

        if (StringUtils.isEmpty(dockerUrl)){
            dockerUrl = DOCKER_URL;
        }

        String policyUrl = "https://" + dockerUrl + POLICY_URL;
        //for ut
        if (StringUtils.isNotEmpty(k8sClusterIp)){
            policyUrl = "https://" + k8sClusterIp + POLICY_URL;
        }
        String policyStr = doHttpGetReq(policyUrl);
        if (StringUtils.isEmpty(policyStr)){
            return null;
        }

        return JSONArray.parseArray(policyStr, PolicyModel.class);
    }

    @Override
    public void syncExistImages(String k8sClusterIp, DestRegistryModel destRegistryModel, String projectName) throws UnsupportedEncodingException {

        /**
         * {"name":"114.115.204.82sync","description":"",
         * "src_registry":null,
         * "dest_registry":{"id":12,"name":"hw01","description":"
         * ","type":"harbor","url":"https://114.115.204.82",
         * "token_service_url":"","credential":{"type":"basic","access_key":"admin","access_secret":"*****"},
         * "insecure":true,"status":"healthy",
         * "creation_time":"2022-09-22T02:41:55.225022Z",
         * "update_time":"2022-09-22T09:38:10.316116Z"},
         * "dest_namespace":null,
         * "trigger":{"type":"manual","trigger_settings":{"cron":""}},
         * "enabled":true,"deletion":false,"override":true,"filters":[{"type":"name","value":"dyq092101/admin/**"}]}
         * **/

        //注册规则
        String policyUrl = "https://" + DOCKER_URL + POLICY_URL;
        HarborPolicyModel harborPolicyModel = new HarborPolicyModel();
        String policyName = k8sClusterIp + "sync";
        harborPolicyModel.setName(policyName);
        harborPolicyModel.setDest_registry(destRegistryModel);

        TriggerModel triggerModel = new TriggerModel();
        triggerModel.setType("manual");
        harborPolicyModel.setTrigger(triggerModel);

        FilterModel filterModel = new FilterModel();
        filterModel.setType("name");
        filterModel.setValue(projectName + "/**");
        harborPolicyModel.setFilters(new ArrayList<>());
        harborPolicyModel.getFilters().add(filterModel);

        String str = JSON.toJSONString(harborPolicyModel);
        doHttpPostReq(policyUrl,new StringEntity(str));

        //做镜像同步
        List<PolicyModel> policyModels = getAllPolicies(null,null);
        for (PolicyModel policyModel:policyModels){
            if(StringUtils.equals(policyModel.getName(),policyName)){

                //做手动复制
                doImageCopy(null,policyModel.getId());
            }
        }
    }

    /**
     *
     */
    @Override
    public void doImageCopy(String k8sClusterIp,String id) throws UnsupportedEncodingException {
        /**
         * https://172.171.11.89/api/v2.0/replication/executions
         * post
         * {"policy_id":16}
         * **/
        String url = "https://" + DOCKER_URL + COPY_URL;
        if (StringUtils.isNotEmpty(k8sClusterIp)){
            url = "https://" + k8sClusterIp + COPY_URL;
        }
        ImageCopyPolicyModel param = new ImageCopyPolicyModel();
        param.setPolicy_id(Integer.parseInt(id));
        String str = JSON.toJSONString(param);
        doHttpPostReq(url,new StringEntity(str));
    }

    /**
     *
     * @param url
     * @param param
     * @return
     */
    private int doHttpPostReq(String url, StringEntity param){

        try {

            // 创建SSL Context，信任所有的Server证书
            SSLContext sslContext = new SSLContextBuilder()
                    .loadTrustMaterial(null, (certificate, authType) -> true).build();
            if (!StringUtils.contains(url, CnapCloudMgmtConstants.TEST_ADMIN)){
                httpClient = null;
            }
            CloseableHttpClient httpClient = getHttpClient(sslContext);

            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Authorization",
                    "Basic "+ Base64.getUrlEncoder().encodeToString((principal + ":" + password).getBytes()));


            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5 * 60 * 1000).setConnectionRequestTimeout(5 * 60 * 1000)
                    .setSocketTimeout(5 * 60 * 1000).build();
            httpPost.setConfig(requestConfig);
            httpPost.setEntity(param);

            CloseableHttpResponse response = httpClient.execute(httpPost);
            //打印结果，供调试
            LOGGER.warn("destUrl={} response.code={} response.entity={}",url,response.getStatusLine().getStatusCode(),
                    EntityUtils.toString(response.getEntity()));
            return response.getStatusLine().getStatusCode();

        }catch (Exception e){
            LOGGER.error("doHttpPostReq fail url={}",url,e);
        }

        return HttpStatus.INTERNAL_SERVER_ERROR.value();
    }

    /**
     *
     * @param url
     * @return
     */
    private String doHttpGetReq(String url){

        try {

            // 创建SSL Context，信任所有的Server证书
            SSLContext sslContext = new SSLContextBuilder()
                    .loadTrustMaterial(null, (certificate, authType) -> true).build();
            //排除ut干扰
            if (!StringUtils.contains(url, CnapCloudMgmtConstants.TEST_ADMIN)){
                httpClient = null;
            }
            CloseableHttpClient httpClient = getHttpClient(sslContext);
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Authorization",
                    "Basic "+ Base64.getUrlEncoder().encodeToString((principal + ":" + password).getBytes()));


            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5 * 60 * 1000).setConnectionRequestTimeout(5 * 60 * 1000)
                    .setSocketTimeout(5 * 60 * 1000).build();
            httpGet.setConfig(requestConfig);

            CloseableHttpResponse response = httpClient.execute(httpGet);
            return EntityUtils.toString(response.getEntity());

        }catch (Exception e){
            LOGGER.error("doHttpGetReq faile url={}",url,e);
        }

        return null;
    }

    /**
     * https://114.115.204.82/api/v2.0/replication/policies/30
     * delete方法
     * 删除策略
     *
     * 获取所有的复制方法
     * https://114.115.204.82/api/v2.0/replication/policies
     * [
     *   {
     *     "id": 29,
     *     "name": "192.144.200.99sync",
     *     "description": "",
     *     "creator": "admin",
     *     "src_registry": {
     *       "id": 0,
     *       "name": "Local",
     *       "description": "",
     *       "type": "harbor",
     *       "url": "http://core:8080",
     *       "token_service_url": "http://core:8080/service/token",
     *       "credential": {
     *         "type": "secret",
     *         "access_key": "",
     *         "access_secret": "*****"
     *       }
     *       ]
     *
     *  删除复制管理
     *  https://114.115.204.82/api/v2.0/registries/15
     */
    @Override
    public void delHarborPolicy(String k8sClusterIp) {

        //因为harbor高可用，需要把去两个harbor删除策略
        List<String> harborUrls = new ArrayList<>();
        harborUrls.add(dockerMasterUrl);
        if ((StringUtils.isNotEmpty(dockerWorkerUrl))
                &&(!StringUtils.equals(dockerWorkerUrl,dockerMasterUrl))){
            harborUrls.add(dockerWorkerUrl);
        }

        //去每个harbor删除
        for(String harborUrl:harborUrls){

            List<PolicyModel> policyModels = getAllPolicies(harborUrl,null);
            List<String> deleteIds = new ArrayList<>();
            //拿到所有复制方法
            for (PolicyModel policyModel:policyModels){
                //方法名里面含有集群ip的就记录
                if (StringUtils.contains(policyModel.getName(),k8sClusterIp)){
                    deleteIds.add(policyModel.getId());
                }
            }

            //去做删除
            for (String deleteId:deleteIds){
                //https://114.115.204.82/api/v2.0/replication/policies/30
                String deleteUrl = "https://" + harborUrl + POLICY_URL + "/" + deleteId;
                doHttpDelReq(deleteUrl);
            }

            //拿到所有的远程仓库链接管理
            DOCKER_URL = harborUrl;

            //拿到所有的复制仓库管理
            List<DestRegistryModel> destRegistryModels = getAllRegisterHarbor(null);
            List<Integer> delHarborIds = new ArrayList<>();
            for (DestRegistryModel destRegistryModel:destRegistryModels){
                if (StringUtils.contains(destRegistryModel.getName(),k8sClusterIp)){
                    delHarborIds.add(destRegistryModel.getId());
                }
            }
            //删除仓库
            for (Integer id :delHarborIds){
                //https://114.115.204.82/api/v2.0/registries/15
                String deleteUrl = "https://" + harborUrl + REGISTRY_URL + "/" + id;
                doHttpDelReq(deleteUrl);
            }
        }
    }


    /**
     *
     * @param url
     * @return
     */
    private String doHttpDelReq(String url){

        try {

            // 创建SSL Context，信任所有的Server证书
            SSLContext sslContext = new SSLContextBuilder()
                    .loadTrustMaterial(null, (certificate, authType) -> true).build();
            //排除ut干扰
            if (!StringUtils.contains(url, CnapCloudMgmtConstants.TEST_ADMIN)){
                httpClient = null;
            }
            CloseableHttpClient httpClient = getHttpClient(sslContext);
            HttpDelete httpDelete = new HttpDelete(url);
            httpDelete.setHeader("Authorization",
                    "Basic "+ Base64.getUrlEncoder().encodeToString((principal + ":" + password).getBytes()));


            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5 * 60 * 1000).setConnectionRequestTimeout(5 * 60 * 1000)
                    .setSocketTimeout(5 * 60 * 1000).build();
            httpDelete.setConfig(requestConfig);

            CloseableHttpResponse response = httpClient.execute(httpDelete);
            return EntityUtils.toString(response.getEntity());

        }catch (Exception e){
            LOGGER.error("doHttpDelReq faile url={}",url,e);
        }

        return null;
    }

    /**
     * 获取userName哈希过后的前8位字符
     *
     * @param userName 用户名
     * @return 结果
     */
    private String getMd5StringWith8(String userName) {
        String md5 = SecureUtil.md5(userName);
        return md5.substring(0, 8);
    }
}
